package cn.edu.nuc.creditSystem.controller.credit;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.creditSystem.bean.credit.Account;
import cn.edu.nuc.creditSystem.bean.credit.Member;
import cn.edu.nuc.creditSystem.bean.user.User;
import cn.edu.nuc.creditSystem.controller.BaseController;
import cn.edu.nuc.creditSystem.service.BaseService;
import cn.edu.nuc.creditSystem.service.credit.service.AccountService;
import cn.edu.nuc.creditSystem.service.credit.service.MemberService;
import cn.edu.nuc.creditSystem.utils.AnalogInterface;
@Controller
@RequestMapping("/cs/credit/member")
public class MemberController extends BaseController<Member>{
	@Autowired
	private MemberService ms;
	@Autowired
	private AnalogInterface ai;
	@Autowired
	private AccountService as;
	@Override
	protected BaseService<Member> getService() {
		return ms;
	}

	@Override
	protected String getPrefix() {
		return "/credit/member";
	}
	@RequestMapping("/tobemember")
	@ResponseBody
	private String toBeMember(HttpSession session){
		try{
			User user=(User)session.getAttribute("user");
			ai.recharge(user.getId(),1000.00,"升级会员，充值1000.00元！");
			user.setAccount(as.get(user.getId()));
			session.setAttribute("user",user);
			return "1";
		}catch(Exception e){
			e.printStackTrace();
		}
		return "-1";
	}
	/**
	 * 充值
	 * @return
	 */
	@RequestMapping("/recharge")
	@ResponseBody
	@Transactional
	private String recharge(Double money,HttpSession session){
		User user=(User)session.getAttribute("user");
		ai.recharge(user.getId(),money,"充值"+money+"元到我的账户");
		user.setAccount(as.get(user.getId()));
		session.setAttribute("user",user);
		return "1";
	}
	/**
	 * 提现
	 * @return
	 */
	@RequestMapping("/drawing")
	@ResponseBody
	@Transactional
	private String drawing(Double money,HttpSession session,String idcard){
		User user=(User)session.getAttribute("user");
		if(!(user.getPassword()+user.getIdcard().substring(user.getIdcard().length()-6,user.getIdcard().length())).equals(idcard))return "0";
		Account a=as.get(user.getId());
		if(a.getMoney()<money)return "-1";
		ai.withdrawals(user.getId(),-money);
		user.setAccount(as.get(user.getId()));
		session.setAttribute("user",user);
		return "1";
	}
}
